from PyQt4 import QtGui

class QTextEditBuffer(QtGui.QTextEdit):
    def __init__(self,parent=None):
        QtGui.QTextEdit.__init__(self,parent)
        # self.tamanho = 0
        
    def inserir(self, texto):
        cursor = self.textCursor()
        # self.tamanho+=len(texto)
        # if self.tamanho > 1000000:
            # cursor.movePosition(QtGui.QTextCursor.Start,QtGui.QTextCursor.MoveAnchor)
            # cursor.movePosition(QtGui.QTextCursor.NextCharacter,QtGui.QTextCursor.KeepAnchor,len(texto))
            # cursor.removeSelectedText()
            # self.tamanho-=len(texto)
            # if self.tamanho < 0:
            # self.tamanho = 0
        scrollBar = self.verticalScrollBar()
        isbaixo = scrollBar.maximum() == scrollBar.value()
        if cursor.atEnd() <> True:
            cursor.movePosition(QtGui.QTextCursor.End,QtGui.QTextCursor.MoveAnchor)
        cursor.insertText(texto)
        
        if isbaixo == True:
            scrollBar.setValue(scrollBar.maximum()) 